const config = require('../config');
const fw = require('../core/framework');
// var ffmpeg = require('ffmpeg');
var ffmpeg = require('fluent-ffmpeg');
// var Clipper = require('image-clipper');
const sharp = require('sharp');
// 文件操作
const fs = require('fs');

module.exports = {
    init(options, callbackFn) {
        let rtmpUrl = config.rtmpRoot + options.code;
    },
    takingPicture(device,callbackFn){
        let filePath = './site/public/screenshots/' + device.merchant_id;
        let rtmpGuid = new fw.utils.GUID().newGUID();
        let screenshotsGuid = new fw.utils.GUID().newGUID();
        let rtmpImg = filePath+'/'+rtmpGuid+'.png';
        let screenshotsImg = filePath+'/'+screenshotsGuid+'.png';
        let process;
        console.log(rtmpImg,screenshotsImg);
        try{
            // 检查文件是否存在，不存在创建文件
            if (!fs.existsSync(filePath)) {
                fs.mkdirSync(filePath)
            }
            try{
                process = new ffmpeg('rtmp://1.116.74.132:1935/live/'+device.code);
            }catch (offlineErr) {
                console('拍照时，rtmp连接异常',offlineErr);
            }
            // let process = new ffmpeg('rtmp://58.200.131.2:1935/livetv/cctv1');  // 测试先用cctv
            process.outputOptions(['-vframes 1']).videoCodec("png").on('error', function (err, stdout, stderr) {
                console.log('拍照时1，摄像头'+device.code+'未连接');
                console.log(err.message);
                callbackFn();
                // log.info('stdout: ' + stdout); log.info('stderr: ' + stderr);
            }).on('complete', function (err, stdout, stderr) {
                console.log('拍照时2，摄像头'+device.code+'未连接');
                console.log(err.message);
                callbackFn();
                // log.info('stdout: ' + stdout); log.info('stderr: ' + stderr);
            }).on('end', function () {
                console.log('Screenshots taken');
                // 对图片进行截图
                sharp(rtmpImg).extract({
                    left: 0,
                    top: 0,
                    width: 640,
                    height: 480
                }).resize(640, 480).toFile(screenshotsImg, (err) => {
                    console.log('----end---');
                    fs.unlink(rtmpImg,function(error){
                        if(error){
                            console.log('删除文件'+rtmpImg+'失败',error);
                            return false;
                        }
                        console.log('删除文件'+rtmpImg+'成功');
                    });
                    if (err) {
                        console.log('---end--error---')
                    }else{
                        callbackFn(screenshotsGuid+'.png');
                    }
                });
                process.kill();
            }).output(rtmpImg, {end: true}).format("image2").run();
        }catch (e) {
            console.log('拍照时发生异常',e);
            callbackFn();
        }
    },
    screenshots() {
        try {
            // var process = new ffmpeg('rtmp://58.200.131.2:1935/livetv/cctv1');
            let imgPath = './screenshots/outputfile.png';
            let imgPath1 = './screenshots/outputfile1.png';
            var process = new ffmpeg('rtmp://1.116.74.132:1935/live/qhy_pi_1');
            process.outputOptions(['-vframes 1']).videoCodec("png").on('error', function (err, stdout, stderr) {
                console.log('error -01 -------')
                console.log(err.message);
                // log.info('stdout: ' + stdout); log.info('stderr: ' + stderr);
            }).on('end', function () {
                console.log('Screenshots taken');
                sharp(imgPath).extract({
                    left: 0,
                    top: 0,
                    width: 640,
                    height: 480
                }).resize(640, 480).toFile(imgPath1, (err) => {
                    console.log('----end---')
                    if (err) {
                        console.log('---end--error---')
                    }
                });
                process.kill();
            }).output(imgPath, {end: true}).format("image2").run();
        } catch (e) {
            console.log('in error---------->')
            console.log(e.code);
            console.log(e.msg);
        }
    }
};
